﻿using ConfExpert.Datasets;
using DevTeam;

using System;
using System.Data;

namespace ConfExpert.Forms.ImportWizards
{
    public partial class XISiparis : XImportWizard
    {
        public XISiparis()
        {
            InitializeComponent();

            this.ProcessStart += XITopCekiListesi_ProcessStart;
            this.ProcessDone += XITopCekiListesi_ProcessDone;
            btnSave.ItemClick += BtnSave_ItemClick;
        }

        private void BtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //DataSet ds = MasterGrid.Dataset();
            //if (ds.Tables[0].Select("ProductId is null").Length > 0)
            //{
            //    ShowErrorMessage("ProductId lerin tamamı dolu olmalı!");
            //    return;
            //}

            string str1 = Ord.Serialize();
            string str2 = Mag.Serialize();

            int res = ExecuteScalar("InsGomlekSiparis", /*YuklemeId, /*idxs,*/ str1, str2).ToInt();       //DİKKAT!
            ShowMessage("Sipariş bilgileri sisteme aktarıldı.");
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        void XITopCekiListesi_ProcessDone()
        {
            /*
            var distinctValues = Mag.AsEnumerable().Select(row => new { Kod = row.Field<string>("Article"), }).Distinct().ToList();
            string codes = string.Join(",", Array.ConvertAll(distinctValues.ToArray(), i => i.Kod));

            DataTable dtId = Session.DataManager.ExecuteDataTable("GetProductIdByCode", codes);
            foreach (DataRow drArticle in Mag.Rows)
            {
                DataRow[] drIds = dtId.Select("Kod='" + drArticle["Article"] + "'");
                if (drIds.Length == 1)
                    drArticle["ProductId"] = (int)drIds[0]["ProductId"];
            }
            
             * 
             * 
            DataTable dtId = AppSession.DataManager.ExecuteDataTable("GetProductIdByCode2", codes);
            foreach (DataRow drArticle in Article.Rows)
            {
                DataRow[] drIds = dtId.Select("Kod='" + drArticle["Article"] + "'");
                if (drIds.Length == 1)
                    drArticle["ProductId"] = (int)drIds[0]["ProductId"];
            }
             * 
             * DESTEX_AF_1 için!
           

            MasterGrid.Init(Mag);
            MasterGrid.AddDetailGrid(DetailGrid, "Article");
            MasterGrid.SutunEkle("TotalQty", typeof(decimal), "SUM(Child({FK}).Qty)");
            MasterGrid.SutunEkle("RollCount", typeof(int), "COUNT(Child({FK}).Article)");
            DetailGrid.MakeColumnInvisible("Article");


            MasterGrid.AddSummary();
            MasterGrid.AddSummary("TotalQty", "RollCount");

            DetailGrid.AddSummary();
            DetailGrid.AddSummary("Qty");

            MasterGrid.OptionsView.ShowViewCaption = true;

            MasterGrid.MakeReadOnly("ProductId");
            DetailGrid.MakeReadOnly();

            MasterGrid.ViewCaption = "";

            DetailGrid.Columns["Qty"].Width = 100;
            DetailGrid.BestFitColumns();
            MasterGrid.BestFitColumns();

            Kontrol();
            */
        }

        public DataTable Mag;
        public MyData.OrdersDataTable Ord;

        void XITopCekiListesi_ProcessStart()
        {
            Mag = new DataTable();
            Mag.Columns.Add("OrderNr", typeof(string));
            Mag.Columns.Add("Ek14", typeof(string));
            Mag.Columns.Add("Ek15", typeof(string));
            Mag.Columns.Add("Artikel", typeof(string));
            Mag.Columns.Add("Model", typeof(string));
            Mag.Columns.Add("MagazaKodu", typeof(string));
            Mag.Columns.Add("Magaza", typeof(string));
            Mag.Columns.Add("DagilimId", typeof(int));
            Mag.Columns.Add("A01", typeof(int));
            Mag.Columns.Add("A02", typeof(int));
            Mag.Columns.Add("A03", typeof(int));
            Mag.Columns.Add("A04", typeof(int));
            Mag.Columns.Add("A05", typeof(int));
            Mag.Columns.Add("A06", typeof(int));
            Mag.Columns.Add("A07", typeof(int));
            Mag.Columns.Add("A08", typeof(int));
            Mag.Columns.Add("A09", typeof(int));
            Mag.Columns.Add("A10", typeof(int));
            Mag.Columns.Add("A11", typeof(int));
            Mag.Columns.Add("A12", typeof(int));
            Mag.Columns.Add("A13", typeof(int));
            Mag.Columns.Add("A14", typeof(int));
            Mag.Columns.Add("A15", typeof(int));
            Mag.Columns.Add("A16", typeof(int));
            Mag.Columns.Add("A17", typeof(int));
            Mag.Columns.Add("A18", typeof(int));
            Mag.Columns.Add("A19", typeof(int));
            Mag.Columns.Add("A20", typeof(int));
            Mag.Columns.Add("A21", typeof(int));
            Mag.Columns.Add("A22", typeof(int));
            Mag.Columns.Add("A23", typeof(int));
            Mag.Columns.Add("A24", typeof(int));
            Mag.Columns.Add("A25", typeof(int));
            Mag.Columns.Add("A26", typeof(int));
            Mag.Columns.Add("A27", typeof(int));
            Mag.Columns.Add("A28", typeof(int));

            Ord = new MyData.OrdersDataTable();

            dtDag = ExecuteDataTable("GetDagilimVertical2");
        }

        public DataTable dtDag;
        /*
        public void Kontrol()
        {
            bool res = false;
            DataTable Prd = new DataTable();

            DataSet d = ExecuteDataSet("CheckTopCeki", YuklemeId, Mag.Serialize());
            if (d.Tables.Count > 0)
            {
                btnSave.Enabled = false;

                using (ArtikelMetrajEsleme popup = new ArtikelMetrajEsleme(d))
                    if (popup.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Prd = popup.d.Tables[1];
                        btnSave.Enabled = true;
                        res = true;
                    }
            }

            if (res)
            {
                foreach (DataRow r in Prd.Rows)
                    MasterGrid.DataTable().Select("Article='" + r["Article"] + "'")[0]["ProductId"] = r["ProductId"];

                MasterGrid.RefreshData();
            }
        }
        */
        private void XISiparis_Load(object sender, EventArgs e)
        {

        }
    }
}